package com.bt.springboot.demo;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zkx
 * @Date 2024/1/31 16:29
 */
public class LockDemo {

	private int num = 100;
	ReentrantLock lock = new ReentrantLock(true);

	public void sale(){
		lock.lock();
		try{
			if(num > 0){
				System.out.println(Thread.currentThread().getName()+"卖出第:\t" + num--);
			}
		}finally{
			lock.unlock();
		}
	}

	public static void main(String[] args) {
		LockDemo lockDemo = new LockDemo();
		new Thread(()->{
			for(int i = 0; i < 100; i++){
				lockDemo.sale();
			}
		},"a").start();
		new Thread(()->{
			for(int i = 0; i < 100; i++){
				lockDemo.sale();
			}
		},"b").start();
		new Thread(()->{
			for(int i = 0; i < 100; i++){
				lockDemo.sale();
			}
		},"c").start();
	}

}
